﻿using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using ThapCam.Data.Collection;
using ThapCam.Ulti;
using System.Dynamic;

namespace ThapCam.System
{
    public abstract class PageBase : Page
    {
        private AbsPageLogic _pageLogic;

        public string group;
        public string pageTitle;
        public string pageDescription;
        public string pageKeyword;
        public string baseHost;
        public string resourceHost;
        public string canonicalLink;
        public bool isRobotsIndex = true;

        public PageBase()
        {
        }

        public AbsPageLogic pageLogic
        {
            get
            {
                return _pageLogic;
            }
            set
            {
                this._pageLogic = value;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            baseHost = WebConfig.HOST_URL;
            resourceHost = WebConfig.HOST_RESOURCE;

            createModuleLogic(this.Context.ApplicationInstance as ThapCamApplication);
        }

        protected virtual void createModuleLogic(ThapCamApplication application)
        {
        }

        public void setCookies(string key, string value)
        {
            if (!Request.Browser.Cookies)
                return;
            HttpCookie cookies = Request.Cookies[CookieKey.COOKIE_NAME];
            if (cookies == null)
                cookies = new HttpCookie(CookieKey.COOKIE_NAME);
            cookies.Expires = DateTime.Now.AddDays(double.Parse(WebConfig.COOKIES_TIME_EXPIRES));
            if (hasCookiesValue(key))
                cookies.Values.Set(key, value);
            else
                cookies.Values.Add(key, value);
            Response.Cookies.Add(cookies);
        }

        public string getCookies(string key)
        {
            string value = null;
            if (!Request.Browser.Cookies)
                return value;
            HttpCookie cookies = Request.Cookies[CookieKey.COOKIE_NAME];
            if (cookies != null)
                value = cookies.Values.Get(key);
            return value;
        }

        public void removeCookies(string key)
        {
            if (!Request.Browser.Cookies)
                return;
            if (hasCookiesValue(key))
            {
                HttpCookie cookies = Request.Cookies[CookieKey.COOKIE_NAME];
                if (cookies != null)
                {
                    cookies.Values.Remove(key);
                    Response.Cookies.Add(cookies);
                }
            }
        }

        public bool hasCookiesValue(string key)
        {
            bool result = false;
            if (getCookies(key) != null)
                result = true;
            return result;
        }

        public string getLinkByType(int id, int type)
        {           
            string link = "";
            if (type == 1)// picture
                link = "chi-tiet/hinh-anh-" + id + ".html";
            else if (type == 2) // video
                link = "chi-tiet/video-" + id + ".html";           
            link = baseHost + link;
            return link.ToLower();
        }

    }
}